import * as api from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['支付宝', '微信', '银行卡'],
    index: 0,
    cashinfo:{account_type:'支付宝',money:'',account:''}
  },
  bindPickerChange: function(e) {
    const cashinfo = this.data.cashinfo
    const array = this.data.array
    cashinfo.account_type = array[e.detail.value]
    this.setData({
      index: e.detail.value,
      cashinfo:cashinfo
    })
  },
  cashrecord:function(){
    wx.navigateTo({
      url: '/pages/business/cashrecord',
    })
  },
  inputEvent(e) {
    const name = e.currentTarget.dataset.name
    const value = e.detail.value.trim()
    let cashinfo = this.data.cashinfo
    cashinfo[name] = value
    this.setData({
      cashinfo
    })
  },
  submit(){
    const that = this
    const cashinfo = that.data.cashinfo
    that.checkCashInfo(cashinfo).then(res => {
      if(res) {
        const money = cashinfo.money
        const account = cashinfo.account
        const account_type = cashinfo.account_type
        api.businessAddWithdraw(money,account,account_type,'')
          .then(res => {
            if(res.code == 1) {
              wx.showToast({
                title: '提交成功,请耐心等待审核',
                icon: 'none'
              })
            }
            setTimeout(function(){
              wx.navigateTo({
                url: '/pages/business/cashrecord',
              })
            },1500)
          }).catch(err => console.log(err))
      }
    }).catch(err => console.log(err))
  },
  checkCashInfo(cashinfo){
    if(cashinfo.money == '') {
      wx.showToast({
        title: '请输入金额',
        icon: 'error'
      })
      return new Promise((resolve, reject) => resolve(false));
    }
    if(cashinfo.account == '') {
      wx.showToast({
        title: '请输入账户',
        icon: 'error'
      })
      return new Promise((resolve, reject) => resolve(false));
    }
    return new Promise((resolve, reject) => resolve(true));
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})